/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.gui.event;

import java.util.LinkedHashSet;

import dgbgui.gui.DefaultCanvasScene;
import dgbgui.gui.widgets.Widget;
import dgbgui.gui.widgets.visual.MaximizeWidget;
import dgbgui.gui.widgets.visual.MinimizeWidget;
import dgbgui.gui.widgets.visual.QueryWidget;
import edu.umd.cs.piccolo.event.PBasicInputEventHandler;
import edu.umd.cs.piccolo.event.PInputEvent;

public class MinimizeMaximizeEventHandler extends PBasicInputEventHandler implements
        CheckConsumedInputEventHandler {


    public void mousePressed(PInputEvent event) {
        
        
        if ( event.getClickCount() == 1 ) {

            // ---------------- MINIMIZE -----------------
            
            LinkedHashSet<Class> objectPreference = new LinkedHashSet<Class>();
            objectPreference.add(MinimizeWidget.class);
            MinimizeWidget minimizeWidget = (MinimizeWidget)Widget.findPreferencialObject((event.getCamera().pick(
                    event.getCanvasPosition().getX(),
                    event.getCanvasPosition().getY(),
                    1)).getNodeStackReference(),
                    objectPreference);
            
            
            
            if ( minimizeWidget != null ) {
                
                // Find the query widget that the minimize belongs to
                QueryWidget queryWidget = (QueryWidget) minimizeWidget.getParentOfClass(QueryWidget.class);
                assert ( queryWidget != null );
                
                // Determine if this is the definition or result minimize widget
                if ( queryWidget.isDefinitionMinMaxWidget(minimizeWidget)) {
                    DefaultCanvasScene.getInstance().setDefinitionExpanded(queryWidget,false);
                } else {
                    DefaultCanvasScene.getInstance().setResultExpanded(queryWidget,false);
                }
                
                m_processed = true;
                // NB return here, otherwise the event just falls through to what 
                // has now become the maximize button
                return;
            }
            
            // ---------------- MAXIMIZE -----------------
            
            objectPreference = new LinkedHashSet<Class>();
            objectPreference.add(MaximizeWidget.class);
            MaximizeWidget maximizeWidget = (MaximizeWidget)Widget.findPreferencialObject((event.getCamera().pick(
                    event.getCanvasPosition().getX(),
                    event.getCanvasPosition().getY(),
                    1)).getNodeStackReference(),
                    objectPreference);
            
            
            
            if ( maximizeWidget != null ) {
                
                // Find the query widget that the maximize belongs to
                QueryWidget queryWidget = (QueryWidget) maximizeWidget.getParentOfClass(QueryWidget.class);
                assert ( queryWidget != null );
                
                // Determine if this is the definition or result minimize widget
                if ( queryWidget.isDefinitionMinMaxWidget(maximizeWidget)) {
                    DefaultCanvasScene.getInstance().setDefinitionExpanded(queryWidget,true);
                } else {
                    DefaultCanvasScene.getInstance().setResultExpanded(queryWidget,true);
                }
                
                m_processed = true;
            }

        }
    }

    public boolean eventProcessed(PInputEvent event, int type) {
        m_processed = false;
        processEvent(event, type);
        return m_processed;
    }

    private boolean m_processed;
}
